/**
  ******************************************************************************
  * @file    debug.c
  * @author  HUGE-IC Application Team
  * @version V1.0.0
  * @date    26-02-2022
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2021 HUGE-IC</center></h2>
  *
  *
  *
  ******************************************************************************
  */ 

/* Includes ------------------------------------------------------------------*/
#include "include.h"

/** @addtogroup Template_Project
  * @{
  */
  
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/



#if (UART_PRINT_EN)


void debug_putchar(u8 uart_data)
{
	while(!(UART1_STA & (1 << 4)));
    UART1_DATA = uart_data;
    while(!(UART1_STA & (1 << 4)));
	
}

/*
void print_u(u32 uData,u8 num)
{
	xdata char out[10];
	xdata int i=0;
	xdata int j;
	do{
		out[i] = 0x30+uData%10;
		i++;
		uData = uData/10;
		if(i>=10) break;
	}while (uData != 0);

	if(num > 0){
		for(j = i; j<num;j++) out[j] = 0x30;
		i = num;
	}

	while(i > 0){
		debug_putchar(out[i-1]);
		i--;
	}
}
*/


//void print_x(u32 uData,u8 num)
//{
//	xdata char out[8];
//	xdata int i=0;
//	xdata int j;
//	do{
//		out[i] = uData%16;
//		if(out[i]<10) out[i] += 0x30;
//		else out[i] += ('A'-10);
//		i++;
//		uData = uData/16;
//		if(i>=8) break;
//	}while (uData != 0);

//	if(num > 0){
//		for(j = i; j<num;j++) out[j] = 0x30;
//		i = num;
//	}

//	debug_putchar('0');
//	debug_putchar('x');
//	while(i > 0){
//		debug_putchar(out[i-1]);
//		i--;
//	}

//	
//}



void LOG_Str(char *pStr)
{
	xdata char temp = *pStr;
	while(temp != 0) {
		debug_putchar(temp); 
		pStr++;
		temp = *pStr;
	}
}

/*

void LOG_StrU(char *str,u32 x)
{
	LOG_Str(str); 
	print_u(x,0); 
	LOG_Str("\r\n");
}
*/

void print_u8X(u8 x)
{
	u8 ch;
	ch = x>>4;
	if(ch<10) ch += 0x30;
	else ch += ('A'-10);
	debug_putchar(ch);
	ch = x&0x0f;
	if(ch<10) ch += 0x30;
	else ch += ('A'-10);
	debug_putchar(ch);
	
}

///*
//void LOG_StrU16X(char *str,u16 x)
//{
//	LOG_Str(str); 
//	print_x(x,4); 
//	LOG_Str("\r\n");
//}


//void LOG_StrU32X(char *str,u32 x)
//{
//	LOG_Str(str); 
//	print_x(x,8); 
//	LOG_Str("\r\n");
//}

//void LOG_E(char *str,u16 x)
//{
//	LOG_Str("\x1b[31m\x1b[1m");
//	LOG_Str(str); 
//	print_x(x,4); 
//	LOG_Str("\x1b[37m\x1b[1m");
//	LOG_Str("\r\n");
//}

//*/

/**
  * @brief  Debug initialization function
  * @param  None
  * @retval None
  */
void debug_init(void)
{
    

	 // UART1 IO : TX--P01(AF1)

    P0_MD0 &= ~0x0C;
    P0_MD0 |=  0x08;
    P0_AF0 |=  0x04;
    
    while(!(UART1_STA & (1 << 4)));
    
    UART1_BAUD1 = (((SYSCLK-DEBUG_BAUD)/DEBUG_BAUD) >> 8) & 0xFF;
    UART1_BAUD0 =  ((SYSCLK-DEBUG_BAUD)/DEBUG_BAUD) & 0xFF;
    UART1_CON   = 0x90;

                   
}

/**
  * @brief  Debug User printf function
  * @param  p_data : The string to send
  * @retval None
  */

#endif

  

/*************************** (C) COPYRIGHT 2021 HUGE-IC ***** END OF FILE *****/
